package com.tencent.qcloud.tuikit.tuichat.zhiyu.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.tencent.qcloud.tuikit.tuichat.R;
import com.tencent.qcloud.tuikit.tuichat.zhiyu.adapter.UserPicAdapter;
import com.ysg.http.data.entity.user.UserResult;
import com.ysg.utils.YCollectionUtil;
import com.ysg.utils.YImageUtil;
import com.ysg.utils.YRecyclerViewUtil;
import com.ysg.utils.YStringUtil;

import java.util.ArrayList;
import java.util.List;

public class UserInfoView extends LinearLayout {
    public interface OnUserInfoListener{
        void onLookInfoClick();
    }

    private OnUserInfoListener listener;
    public void setOnUserInfoListener(OnUserInfoListener listener){
        this.listener = listener;
    }


    public UserInfoView(Context context) {
        super(context);
        init();
    }

    public UserInfoView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public UserInfoView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    public ImageView ivAvatar;
    public TextView tvName;
    private TextView tvAge;
    private TextView tvHeight;
    private TextView tvProfession;
    public TextView tvSignature;
    private RecyclerView recyclerPic;
    private UserPicAdapter adapter;
    private void init() {
        View userInfoCardView = inflate(getContext(), R.layout.chat_user_info, this);
        ivAvatar = userInfoCardView.findViewById(R.id.ivAvatar);
        tvName = userInfoCardView.findViewById(R.id.tvName);
        tvAge = userInfoCardView.findViewById(R.id.tvAge);
        tvHeight = userInfoCardView.findViewById(R.id.tvHeight);
        tvProfession = userInfoCardView.findViewById(R.id.tvProfession);
        tvSignature = userInfoCardView.findViewById(R.id.tvSignature);

        recyclerPic = userInfoCardView.findViewById(R.id.recyclerPic);
        adapter = new UserPicAdapter();
        YRecyclerViewUtil.initHorizontalNoLine(getContext(), recyclerPic, adapter);

        userInfoCardView.findViewById(R.id.ivClose).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                userInfoCardView.findViewById(R.id.viewClose).setVisibility(GONE);
            }
        });
        userInfoCardView.findViewById(R.id.tvLookInfo).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null){
                    listener.onLookInfoClick();
                }
            }
        });
        userInfoCardView.findViewById(R.id.ivAvatar).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null){
                    listener.onLookInfoClick();
                }
            }
        });
    }

    public void setData(UserResult user){
        YImageUtil.show(ivAvatar, user.getPic());
        tvName.setText(user.getNick());
        if (YStringUtil.isNotEmpty(user.getAge()+"")){
            tvAge.setText(user.getAge() + getResources().getString(R.string.year_old));
            tvAge.setVisibility(VISIBLE);
        }
        if (YStringUtil.isNotEmpty(user.getShengao())){
            tvHeight.setText(user.getShengao());
            tvHeight.setVisibility(VISIBLE);
        }
        if (YStringUtil.isNotEmpty(user.getZhiyename())){
            tvProfession.setText(user.getZhiyename());
            tvProfession.setVisibility(VISIBLE);
        }
        if (YStringUtil.isNotEmpty(user.getMysign())) {
            tvSignature.setText(user.getMysign());
        }

        List<String> imageList = new ArrayList<>();
        if (YStringUtil.isNotEmpty(user.getXingxiang())) {
            String[] split = user.getXingxiang().split(",");
            for (int i = 0; i < split.length; i++) {
                imageList.add(split[i]);
            }
            if (YCollectionUtil.isNotEmpty(imageList)){
                adapter.setList(imageList);
                recyclerPic.setVisibility(VISIBLE);
            }
        }

    }



}
